package com.by.hellosecurityresource.controller;

import com.by.hellosecurityresource.domain.STest;
import com.by.hellosecurityresource.service.TestService;
import com.by.hellosecurityresource.controller.request.TestRequestBean;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 白羽
 * @Date 2022-12-12 12:56:52
 */
@Api(tags = "测试需要登录验证才能访问的接口")
@RestController
@RequestMapping("/test")
public class TestController {
    /**
     * 服务对象
     */
    private TestService testService;

    public TestController(TestService testService) {
        this.testService = testService;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "根据id获取test数据")
    @PreAuthorize("hasAuthority('test:test:get')")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public STest getTestById(@PathVariable("id") Long id) {
        return testService.findById(id);
    }

    @Operation(summary = "获取test列表数据")
    @PreAuthorize("hasAuthority('test:test:get')")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Page<STest> list(@RequestParam @Parameter(description = "页数从1开始", required = true) int page,
                            @RequestParam @Parameter(description = "每页的条数", required = true) int limit,
                            HttpServletRequest request) {

        return testService.list(page - 1, limit);

    }

    @Operation(summary = "创建test数据")
    @PreAuthorize("hasAuthority('test:test:create')")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public STest createTest(@RequestBody TestRequestBean bean, HttpServletRequest request) {
        return testService.create(bean);
    }

    @Operation(summary = "根据id修改test数据")
    @PreAuthorize("hasAuthority('test:test:update')")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public STest updateTestById(@PathVariable("id") Long id, @RequestBody TestRequestBean bean, HttpServletRequest request) {
        return testService.updateById(id, bean);
    }

    @Operation(summary = "根据id删除test数据")
    @PreAuthorize("hasAuthority('test:test:delete')")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteTestById(@PathVariable("id") Long id, HttpServletRequest request) {
        testService.deleteById(id);
    }

}